
import { componentFuncConfig } from '../config/component'
import { treeToArray } from '@/utils/util'
import store from '@/store'
export function getComponentParams (comType) {
  const config = componentFuncConfig[comType]
  return config.params
}
export function transformLayoutTree (list) {
  return list.map((i) => {
    // hlLayout栅格布局
    if (i.tagName === 'hlLayout') {
      const children = i.children
        .filter((j) => j.children.length > 0)
        .map((z) => z.children).flat()
      return Object.assign({}, i, { children: transformLayoutTree(children) })
    } else if (i.children?.length) {
      return Object.assign({}, i, {
        children: transformLayoutTree(i.children)
      })
    } else {
      return i
    }
  })
}

// 获取树结构中的表单数据  --- 模态框中
export function getModalFormListInTree ({ getModelTreeList }) {
  let queen = []
  const out = []
  queen = queen.concat(getModelTreeList)
  while (queen.length) {
    const first = queen.shift()
    if (first.tagName === 'hlLayoutForm') {
      const { modelCode, moduleId } = first.__config
      out.push({
        key: first.key,
        label: `${first.name}(${first.key})`,
        modelCode,
        moduleId
      })
    } else if (first?.children?.length > 0) {
      queen = [].concat(first.children, queen)
    }
  }
  return out
}
export function getAllComponentList () {
  let list = store.getters.getComponentListByPage // 所有
  list = list.filter(item => !!item.key)
  // const other = []
  // list= list.filter(comp => !other.includes(comp.tagName))
  return list
}

// 获取所有的表单列表
export function getAllFormList () {
  const list = store.getters.getComponentListByPage
  return list.filter(item => item.tagName === 'hlLayoutForm')
}

export function getAllTableList () {
  const list = store.getters.getComponentListByPage
  return list.filter(item => item.tagName === 'hlTableList')
}

export function getAllModalList () {
  const modalList = store.getters.getFormDataForMain?.template || [] // modal

  return modalList
}

// 获取所有modal中的组件
export function getAllModalComponent () {
  let modalComList = []
  const modalList = store.getters.getFormDataForMain?.template || [] // modal
  modalList.forEach((template) => {
    // 模态框的大纲树
    const getModelTreeList = transformLayoutTree(template.formList || [])
    // 模态框中的表单list
    const comList = treeToArray(getModelTreeList)
    modalComList = modalComList.concat(comList)
  })
  return modalComList
}

export function getAllReloadComponent () {
  const reloadArr = ['hlTree', 'hlSelectTree', 'hlTableList', 'hlMindmap', 'hlCalendar']
  const list = getAllComponentList()
  return list.filter(item => reloadArr.includes(item.tagName))
}

// 获取所有有getData方法的组件
export function getAllGetDataComponent () {
  const typeArr = ['hlSelect', 'hlTableList', 'hlProcess', 'hlCalendar']
  const list = getAllComponentList()
  return list.filter(item => typeArr.includes(item.tagName))
}

// 获取所有有addSearchData的组件
export function getAllAddSearchDataComponent () {
  const typeArr = ['hlSelect', 'hlMindmap', 'hlTableList', 'hlTree', 'hlSelectTree', 'hlCalendar']
  const list = getAllComponentList()
  return list.filter(item => typeArr.includes(item.tagName))
}

// 获取组件的label
export function getComLabel (com) {
  const label = (com?.modelName) ? (com?.__config?.label) : ''
  return com.name + label + '（' + com.key + '）'
}
